import http from 'http'
import Koa from 'koa'
import logger from 'koa-logger'      // 请求日志
import cors from '@koa/cors'         // 允许跨域
import { koaBody } from 'koa-body'
import baseRoute from './routes'
import result from './middlewares/Result'
import errorHandle from './middlewares/ErrorHandle'
import notFound from './middlewares/NotFound'
import { Server } from 'socket.io'
import GameNotifier from './websocket/GameNotifier'
// import parameter from 'koa-parameter'

const app = new Koa();
const httpServer = http.createServer(app.callback())
const io = new Server(httpServer, {
  // socket跨域处理
  cors: {
    origin: "http://localhost:7456" // 客户端域名地址
  }
})

app.proxy = true

// 中间件 middlewares
app.use(logger())
app.use(cors())
app.use(koaBody({
  multipart: true
}))
app.use(result)       // 装载http响应结果函数
app.use(errorHandle)  // err

// socket
GameNotifier.instance.init(io)

// routes
app.use(baseRoute.routes()).use(baseRoute.allowedMethods())

// 404
app.use(notFound)

httpServer.listen(3000);

console.log('Server running on http://localhost:3000');